// ml:run = $bin < input
#include <iostream>
#include <algorithm>

using ll = long long;
int const maxn = 100007;
int father[maxn], a[maxn], b[maxn];
ll ans[maxn];
bool vis[maxn];
ll sum[maxn];
ll max;
int n;

int get_father(int x)
{
    return x == father[x] ? x : (father[x] = get_father(father[x]));
}

void set_union(int u, int v)
{
    int tu = get_father(u);
    int tv = get_father(v);
    if (tu != tv) {
        father[tu] = tv;
        sum[tv] += sum[tu];
        max = std::max(sum[tv], max);
    }
}

int main()
{
    std::ios_base::sync_with_stdio(false);
    std::cin >> n;
    for (int i = 1; i <= n; i++) {
        father[i] = i;
        std::cin >> a[i];
    }
    for (int i = 1; i <= n; i++) std::cin >> b[i];

    ans[n] = max;
    for (int i = n; i >= 2; i--) {
        int t = b[i];
        max = std::max(max, (ll)a[t]);
        sum[t] = a[t];
        vis[t] = true;
        if (t > 1 && vis[t - 1]) {
            set_union(t, t - 1);
        }
        if (t < n && vis[t + 1]) {
            set_union(t, t + 1);
        }
        ans[i - 1] = max;
    }
    for (int i = 1; i <= n; i++)
        std::cout << ans[i] << "\n";
}

